package cn.doctorpda.conference.presenter;

import org.json.JSONException;
import org.json.JSONObject;

import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.search.PageConInfo;
import cn.doctorpda.conference.bean.search.PageSearchInfo;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.config.NetConfig;
import cn.doctorpda.conference.model.GetSearchActivityModel;
import cn.doctorpda.conference.model.GetSearchConModel;
import cn.doctorpda.conference.model.IPostModel;
import cn.doctorpda.conference.okhttp.callback.StringCallback;
import cn.doctorpda.conference.utils.AESUtil;
import cn.doctorpda.conference.utils.GsonUtil;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.view.callbackview.ISearchConPageView;
import cn.doctorpda.conference.view.callbackview.ISearchPageView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/19.
 */
public class SearchConPresenter {
    private IPostModel model;
    private ISearchConPageView searchConPageView;
    private PageConInfo info = null;

    public SearchConPresenter(ISearchConPageView baseView) {
        model = new GetSearchConModel();
        searchConPageView = (ISearchConPageView) baseView;
    }

    public void getDocData(final String page, String limit, String q, String type
    ) {
        model.postNetWorkStringData(NetConfig.GET_SEARCH_ACTIVITY, new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                searchConPageView.setPageError(call, e);
            }

            @Override
            public void onResponse(String response) {
                try {
                    Msg msg = GsonUtil.fromJson(response, Msg.class);
                    JSONObject jsonObject = new JSONObject(response);
                    L.i("搜索会议" + response);
                    if (jsonObject.getInt("code") == 0) {
                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
                        info = GsonUtil.fromJson(data, PageConInfo.class);
                        L.i(data.toString());
                    } else {
                        info = GsonUtil.fromJson(msg.getData(), PageConInfo.class);
                    }
                    if (page.equals("1")) {
                        searchConPageView.setPageRefreshResult(msg, info);
                    } else {
                        searchConPageView.setPageLoadResult(msg, info);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, page, limit, q, type);
    }


}
